"use strict";
/* Controllers */
app.controller("timeoutTaskListController", ['$scope', '$location', '$rootScope', '$routeParams', 'timeoutTaskListService', function ($scope, $location, $rootScope, $routeParams, timeoutTaskListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listTimeoutTask = function () {
        var vm = $scope.vm;
        var params = $scope.vm.p;
        $('#list_table').bootstrapTable('showLoading');
        $("#refreshTable").addClass('disabled');
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
        };
        console.log(params);
        timeoutTaskListService.listTimeoutTask(params, procFn, errorFn);
    };

    function getIdSelections() {
        return $.map($('#list_table').bootstrapTable('getSelections'), function (row) {
            return row.id
        })
    }

    $scope.BatchConfirmDelayKill = function (id, delayTime) {

        var ids = getIdSelections();

        if (ids.length <= 0) {
            $scope.loading = true;
            $scope.vm.p.showNoSelect = true
        } else {
            $scope.loading = false;
            $scope.vm.p.showNoSelect = false
        }

        $scope.vm.toDelayKillIds = ids;
        $scope.vm.delayTime = 3600;
        $scope.$apply();
        $('#delayKillModal').modal('show')
    };

    $scope.delayKillOk = function () {
        $scope.loading = true
        var vm = $scope.vm;
        console.log("toDelayKillId" + vm.toDelayKillId);
        var params = {};
        params.ids = vm.toDelayKillIds;
        params.delayTime = vm.delayTime
        var procFn = function (response) {
            $scope.loading = false
            $('#delayKillModal').modal('hide')
            if (response.success && response.data) {
                alert("配置成功")
            } else {
                alert(response.msg);
            }
        };
        timeoutTaskListService.delayKillConfig(params, procFn, errorFn);

    };

    $scope.confirmDel = function (id, appId, index) {
        vm.toDelId = id;
        vm.toKillAppId = appId;
        vm.toDelIndex = index;
        $('#myModal').modal('show')
    };

    $scope.ok = function () {
        var vm = $scope.vm;
        console.log("toDelId" + vm.toDelId);
        console.log("toDelIndex" + vm.toDelIndex);
        var params = {};
        params.appId = vm.toKillAppId;
        var procFn = function (response) {
            if (response.success && response.data) {
                // $('#list_table').bootstrapTable('remove', {
                //     field: 'id',
                //     values: [vm.toDelId]
                // });
                alert("kill成功")
            } else {
                alert(response.msg);
            }
        };
        timeoutTaskListService.killTimeoutTask(params, procFn, errorFn);
        $('#myModal').modal('hide')
    };

    $("#refreshTable").click(function () {
        $('#list_table').bootstrapTable('refresh');
        $scope.listTimeoutTask()
    });

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [100, 300, 500, 100], pageSize: 100
        });
        $resultTable.bootstrapTable('hideColumn', 'user');
        $resultTable.bootstrapTable('hideColumn', 'container');
//        $resultTable.bootstrapTable('hideColumn', 'action');

        $scope.listTimeoutTask();
    };

    $scope.initializeController();

}]);

/* Services */
app.service('timeoutTaskListService', ['ajaxService', function (ajaxService) {

    this.listTimeoutTask = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/task/listTimeoutTask.do", successFunction, errorFunction);
    };

    this.killTimeoutTask = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/task/killTimeoutTask.do", successFunction, errorFunction);
    };
    this.delayKillConfig = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/task/delayKillConfig.do", successFunction, errorFunction);
    };

}]);

function optStatusFmt(value, row, index) {
    if (value == 1) {
        return "待KILL"
    } else if (value == 2) {
        return "已延后"
    } else if (value == 3) {
        return "已KILL"
    }
}

function delayTimeFormat(value, row, index) {
    return useTimeFormat(value * 1000, row, index)
}

function timeoutTaskOptFormat(value, row, index) {
    var optCont0 = '<div style="width:11rem"><a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel("' + row.id + '","' + row.appId + '",' + index + ') >KILL</a>\n'
    // var optCont1 = '<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDelayKill("' + row.id + '",' + row.delayTime + ') >推迟KILL</a>\n'
    var optCont2 = '<a type="button" class="btn btn-xs btn-primary" href="' + row.trackingUrl + '" target="_blank">任务详情</a></div>'

    return [
        optCont0,
        optCont2
    ].join('');
}

function storeFormat(value, row, index) {
    // return parseInt(value) / 1024.0 + "GB"
    return toThousands(value) + "MB"
}
